import gtk          # Calls gtk module
import gtk.gtkgl        # Calls gtk.gtkgl module

from OpenGL.GL import *
from OpenGL.GLU import *



def polyline( vertexes_list, closed = False):
    if closed:
        glBegin( GL_LINE_LOOP )
    else:
        glBegin( GL_LINE_STRIP )
    
    for vertex in vertexes_list:
        glVertex3f( vertex[0], vertex[1], 0 )
    
    glEnd()
    
class DrawArea(gtk.DrawingArea, gtk.gtkgl.Widget):
    def __init__(self):
        gtk.DrawingArea.__init__(self)
        print 'ok'
        
        self.set_events( gtk.gdk.BUTTON_MOTION_MASK | gtk.gdk.KEY_PRESS_MASK |
                         gtk.gdk.KEY_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK |
                         gtk.gdk.BUTTON_RELEASE_MASK| gtk.gdk.BUTTON_PRESS_MASK |
                         gtk.gdk.SCROLL_MASK )
        display_mode = ( gtk.gdkgl.MODE_RGB | gtk.gdkgl.MODE_DOUBLE )
        try:                              
            glconfig = gtk.gdkgl.Config(mode=display_mode)
        except gtk.gdkgl.NoMatches:
            display_mode &= ~gtk.gdkgl.MODE_SINGLE
            glconfig = gtk.gdkgl.Config(mode = display_mode) 
            self.set_gl_capability(glconfig)
            self.set_events( gtk.gdk.BUTTON_MOTION_MASK | gtk.gdk.KEY_PRESS_MASK |
                             gtk.gdk.KEY_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK |
                             gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.BUTTON_PRESS_MASK |
                             gtk.gdk.SCROLL_MASK )
            '''
            self.connect( "expose_event", self.on_expose_event )
            self.connect( "realize", self.on_realize )
            self.connect( "configure_event", self.on_configure_event )
            '''
        
    def on_configure_event(self, *args):
        gldrawable = self.get_gl_drawable()
        glcontext = self.get_gl_context()
        gldrawable.gl_begin(glcontext)      
        glViewport(0, 0, 1000, 800)
        gluLookAt(0.0,0.0,0.5, 0.0,0.0,0.0, 0.0, 1.0, 0.0)

            
    def on_realize(self,*args):
        gldrawable = self.get_gl_drawable()       # Obtain a reference to the Grafica drawable
        glcontext = self.get_gl_context()         # and rendering context.
        if not gldrawable.gl_begin(glcontext):   # inizio dei comandi Grafica
            return
        glClearColor(1.0, 1.0, 1.0, 1.0)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho( -40, 40, -30, 30, -1, 1 )
        gldrawable.gl_end()                       #fine dei comandi Grafica
  

    def on_expose_event(self, *args):
        gldrawable = self.get_gl_drawable()
        glcontext  = self.get_gl_context()
        gldrawable.gl_begin(glcontext)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        self.display()
        gldrawable.swap_buffers()
        gldrawable.gl_end()
        
    def display(self):
        print 'a'
        glClearColor(0,0,0,0)
        glClear(GL_COLOR_BUFFER_BIT)
        glColor3f(1,1,0)
        polyline( [ ( -10, -10 ), ( 10, -10 ), ( 10, 10 ), ( -10, 10 ) ], closed = True )
    

